#include <cstdlib>
#include <iostream>
#include <list>
#include "ourhdr.h"

Pixel& Object::operator[](std::size_t n){
	if(n >= m_pixels.size()){
		std::cerr << "Overflow!\n";
		exit(1);
	}
	int i = 0;
	for(auto beg = m_pixels.begin(); beg != m_pixels.end(); beg++){
		if(i == n)
			return *beg;
		i++;
	}
}

int Object::count() const{
	return m_pixels.size();
}

bool Object::hasPos(unsigned row, unsigned col) const{
	for(auto beg = m_pixels.begin(); beg != m_pixels.end(); beg++){
		if(row == beg->row() && col == beg->col())
			return true;
	}
	return false;
}

bool Object::hasPos(const Pos &pos) const{
	for(auto beg = m_pixels.begin(); beg != m_pixels.end(); beg++){
		if(beg->pos() == pos)
			return true;
	}
}

void Object::castTo(Screen &screen) const{
	for(auto &i : m_pixels){
		screen.setPixel(i.row(), i.col(), i.character());
	}
}

bool Object::overlapWith(const Object &obj) const{
	for(auto beg = obj.m_pixels.begin(); beg != obj.m_pixels.end(); beg++){
		if(hasPos(beg->row(), beg->col()))
			return true;
	}
	return false;
}

void Object::addBack(const Pixel &pixel){
	m_pixels.push_back(pixel);
}

void Object::addFront(const Pixel &pixel){
	m_pixels.push_front(pixel);
}

bool Object::del(const Pixel &p){
	   for(auto beg = m_pixels.begin(); beg != m_pixels.end(); beg++){
		   if(p == *beg){
			   m_pixels.erase(beg);
			   return true;
		   }
	   }
	   return false;
}

void Object::delBack(){
	m_pixels.pop_back();
}

void Object::delFront(){
	m_pixels.pop_front();
}
